/**
 * 
 */
package ro.dta.idbi.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JDialog;
import javax.swing.JPanel;

import ro.dta.idbi.interfaces.ui.IMainFrame;
import ro.dta.idbi.model.dynamicpattern.DependencyGraph;
import ro.dta.idbi.model.dynamicpattern.Transaction;

import com.mxgraph.swing.mxGraphComponent;

/**
 * Dependency graph viewer dialog, visualise the dependency graph of a
 * transaction
 * 
 * @author Tudor Dobrila
 * 
 */
public class TransactionViewerDialog extends JDialog {

	private static final long serialVersionUID = 3145036584847949077L;

	/**
	 * Dependency graph viewer class constructor
	 */
	public TransactionViewerDialog(IMainFrame parentFrame, Transaction transaction) {
		super();
		setSize(800, 600);
		setTitle("Transaction signature (Dependency graph) viewer [Transaction = " + transaction.getTid() + "]");
		setModal(true);
		setLocationRelativeTo(null);
		setIconImage(parentFrame.getIconImage());

		DependencyGraph graph = transaction.getSignature();
		graph.performLayout();
		final mxGraphComponent comp = new mxGraphComponent(graph);
		comp.getViewport().setBackground(Color.WHITE);

		final JPanel centerPanel = new JPanel(new GridBagLayout());
		getContentPane().add(centerPanel, BorderLayout.CENTER);
		
		GridBagConstraints constr = new GridBagConstraints();
		constr.fill = GridBagConstraints.BOTH;

		constr.gridx = 0;
		constr.gridy = 0;
		constr.weightx = 1.0;
		constr.weighty = 0.0;
		constr.ipady = 20;
		constr.gridwidth = 3;
		centerPanel.add(parentFrame.getGuiFactory()
				.createTitleLabel("Instructions in transaction:"), constr);

		constr.gridx = 0;
		constr.gridy = 1;
		constr.weightx = 1.0;
		constr.weighty = 0.4;
		StringBuilder builder = new StringBuilder();
		String[] instructions = transaction.getInstructions();
		for (String instr : instructions) {
			builder.append(instr).append("\n");
		}
		final LineTextArea lineTextArea = new LineTextArea();
		lineTextArea.getTextArea().setText(builder.toString());
		lineTextArea.getTextArea().setEditable(false);
		centerPanel.add(lineTextArea, constr);

		constr.gridx = 0;
		constr.gridy = 2;
		constr.weightx = 1.0;
		constr.weighty = 0.0;
		centerPanel.add(parentFrame.getGuiFactory().createTitleLabel(
				"Signature of the transaction :"), constr);

		constr.gridx = 0;
		constr.gridy = 3;
		constr.weightx = 1.0;
		constr.weighty = 0.6;
		centerPanel.add(comp, constr);
	}

}
